package com.czl.module_user.viewmodel

import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.AddRecordBean
import com.czl.base.data.bean.RepairListBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

/**
 *
 * @Description:
 * @Author: XCH
 * @CreateDate: 2022/1/7 8:43
 */
class AddRecordsViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    val uc = UiChangeEvent()

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<List<AddRecordBean>> = SingleLiveEvent()
        val deleteSuccessEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val deleteEvent: SingleLiveEvent<String> = SingleLiveEvent()
        val renewEvent: SingleLiveEvent<AddRecordBean> = SingleLiveEvent()
    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getMonthCardList()
    })
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getMonthCardList()
    })

    private fun getMonthCardList() {
        model.getApplyRecords(model.getAreaId())
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<List<AddRecordBean>>>() {
                override fun onResult(t: BaseBean<List<AddRecordBean>>) {
                    if (t.code == 200) {
                        uc.loadCompleteEvent.postValue(t.data)
                    } else {
                        uc.loadCompleteEvent.postValue(null)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    uc.loadCompleteEvent.postValue(null)
                }

            })
    }

    fun deleteApply(id: String) {
        model.deleteApply(id)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                override fun onResult(t: BaseBean<Any?>) {
                    if (t.code == 200) {
                        uc.deleteSuccessEvent.call()
                        showSuccessToast(t.msg)
                    } else {
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                }

            })
    }
}